//
//  SDPhotoDetailView.m
//  SKinDiary
//
//  Created by 廖海 on 2017/4/26.
//  Copyright © 2017年 tencent. All rights reserved.
//

#import "SDPhotoDetailView.h"
#import "SDPhotoFullScreenViewController.h"
#import "SDPaientInfoViewController.h"
#import "SDPhotoInfoViewController.h"
#import "SDPhotoDetailInfoCell.h"
#import "SDPhotoCommentCell.h"
#import "SDPhotoPreview.h"
#import "SDPaientInfoMgr.h"
#import "SDPaientInfoItem.h"
#import "AAPLCameraViewController.h"
#import "AppDelegate.h"

@interface SDPhotoDetailView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView           *tableView;

@end
@implementation SDPhotoDetailView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor colorWithRGB:0xf9fafc];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_tableView];
    }
    return self;
}

- (UINavigationController *)rootViewController
{
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if ([appDelegate.window.rootViewController isKindOfClass:[UINavigationController class]]) {
        return (UINavigationController *)appDelegate.window.rootViewController;
    } else {
        return appDelegate.window.rootViewController.navigationController;
    }
}

-(void)loadData
{
    if (_photoInfo != nil) {
        NSArray* photoList = [[SDPaientInfoMgr shareAGDatabaseManager] select:[SDPhotoInfo class] tableName:kPhotoTableName where:[NSString stringWithFormat:@"id = %d",_photoInfo.id]];
        if (CHECK_VALID_ARRAY(photoList)) {
            _photoInfo = [photoList objectAtIndex:0];
            [self.tableView reloadData];
        }
    }
}
- (void)setPhotoInfo:(SDPhotoInfo *)photoInfo
{
    _photoInfo = photoInfo;
    [self loadData];
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    if (row == 0) {
        return kPhotoCellHeight;
    } else if(row == 1){
        return [SDPhotoCommentCell getRequiredCellHeight:_photoInfo.comments];
    }
    else if(row == 2){
        return kPhotoDetailInfoCellHeight;
    }else if(row == 3){
        return kPhotoDetailInfoCellHeight;
    } else if(row == 4){
        return kPhotoDetailInfoCellHeight;
    } else if(row == 5){
        return kPhotoDetailInfoCellHeight;
    } else if(row == 6){
        return kPhotoDetailInfoCellHeight;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    
    if (row == 0) {
        SDPhotoPreview* cell = nil;
        static NSString* identifier = @"photoCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[SDPhotoPreview alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [cell setPhotoInfo:_photoInfo];
        return cell;
    }else if (row == 1) {
        SDPhotoCommentCell* cell = nil;
        static NSString* identifier = @"commentCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[SDPhotoCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [cell setCellData:@"备注" subtitle:_photoInfo.comments];
        cell.arrowLabel.hidden = NO;
        return cell;
    }
    else if (row == 2) {
        SDPhotoDetailInfoCell* cell = nil;
        static NSString* identifier = @"paientCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[SDPhotoDetailInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        NSString* paientName = nil;
        if (_photoInfo.patientId  > 0 ) {
            NSArray* paientList = [[SDPaientInfoMgr shareAGDatabaseManager] select:[SDPaientInfoItem class] tableName:kPaientTableName where:[NSString stringWithFormat:@"pId = %ld",_photoInfo.patientId]];
            if (CHECK_VALID_ARRAY(paientList)) {
                SDPaientInfoItem* paientItem = [paientList objectAtIndex:0];
                paientName = paientItem.name;
            }
        }
        
        [cell setCellData:@"患者" subtitle:paientName];
        cell.arrowLabel.hidden = NO;
        
        return cell;
    }else if (row == 3) {
        SDPhotoDetailInfoCell* cell = nil;
        static NSString* identifier = @"tagsCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[SDPhotoDetailInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [cell setCellData:@"标签" subtitle:_photoInfo.tag];
        cell.arrowLabel.hidden = NO;
        
        return cell;
    } else if (row == 4) {
        SDPhotoDetailInfoCell* cell = nil;
        static NSString* identifier = @"positionCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[SDPhotoDetailInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [cell setCellData:@"部位" subtitle:_photoInfo.position];
        
        return cell;
    } else if (row == 5) {
        SDPhotoDetailInfoCell* cell = nil;
        static NSString* identifier = @"creationTimeCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[SDPhotoDetailInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [cell setCellData:@"创建" subtitle:[SDNSString getTimeStringFromDateString:_photoInfo.creationTime]];
        
        return cell;
    } else if (row == 6) {
        SDPhotoDetailInfoCell* cell = nil;
        static NSString* identifier = @"updateTimeCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[SDPhotoDetailInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        [cell setCellData:@"更新" subtitle:[SDNSString getTimeStringFromDateString:_photoInfo.updateTime]];
        
        return cell;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    if(row == 0)
    {
        SDPhotoFullScreenViewController* fullScreenVC = [[SDPhotoFullScreenViewController alloc] init];
        fullScreenVC.photoInfo = _photoInfo;
        [[self rootViewController] pushViewController:fullScreenVC animated:YES];
    }
    else if(row == 1)
    {
        SDPhotoInfoViewController* photoInfoVC = [[SDPhotoInfoViewController alloc] init];
        photoInfoVC.photoInfoArray = @[_photoInfo];
        photoInfoVC.locateCommentArea = YES;
        [[self rootViewController] pushViewController:photoInfoVC animated:YES];
    }
    else if(row == 2)
    {
        SDPhotoInfoViewController* photoInfoVC = [[SDPhotoInfoViewController alloc] init];
        photoInfoVC.photoInfoArray = @[_photoInfo];
        [[self rootViewController] pushViewController:photoInfoVC animated:YES];
    }
    else if(row == 3)
    {
        SDPhotoInfoViewController* photoInfoVC = [[SDPhotoInfoViewController alloc] init];
        photoInfoVC.photoInfoArray = @[_photoInfo];
        [[self rootViewController] pushViewController:photoInfoVC animated:YES];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
